import { login, getUser } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

const state = {
  token: getToken(),
  user: {}
}
const mutations = {
  handleToken(state, newtoken) {
    state.token = newtoken
    setToken(newtoken)
  },
  getuser(state, newuser) {
    state.user = newuser
  }
}
const actions = {
  async loginAction(ctx, data) {
    const res = await login(data)
    ctx.commit('handleToken', res)
  },
  async getUserAction(ctx) {
    const res = await getUser()
    ctx.commit('getuser', res)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

